package com.rabbitmq.typic;

import com.rabbitmq.client.*;

/**
 * @author yinxing
 * @date 2022-08-29
 */
public class Consumer {

    private static final String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception{
        //创建连接工厂
        ConnectionFactory factory =new ConnectionFactory();
        factory.setHost("192.168.72.128");
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setPort(5672);
        //创建连接
        Connection connection = factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();
        //声明 接收消息
        DeliverCallback deliverCallback = (consumerTag, message) ->{
            System.out.println("接收到消息:"+ new String(message.getBody(),"utf-8"));
        };
        //声明 取消消息
        CancelCallback cancelCallback = (consumerTag)->{
            System.out.println("消息消费被中断");
        };
        /**
         * 接收消息，消费
         * 1.消费那个队列
         * 2.消费成功是否自动应答，true or false
         * 3.消费者未成功消费回调
         * 4.消费者取消消费的回调
         */
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);
    }
}
